const {
  taskLogger
} = require("./log");

class Task {
  constructor(orderId, data) {
    this.taskType = "wechatfast";
    this.orderId = orderId;
    this.data = data || this.orderId;
    this.createTime = new Date().getTime();
  }
}

class PayingTask extends Task {
  constructor(orderId, data) {
    super(orderId, data);
    this.status = "paying";
    this.count = 0;
  }
  selfCheck() {
    const now = new Date().getTime();
    const interval = now - this.createTime;
    //大于5分钟
    if (this.count > 3) {
      this.onChecked && this.onChecked(false);
      taskLogger.fatal({
        task: this
      })
    } else if (interval > 5 * 60 * 1000) {
      this.count++;
      this.onChecked && this.onChecked(true);
    }
  }
}

exports.PayingTask = PayingTask;



class NoticedTask extends Task {
  constructor(orderId, data) {
    super(orderId, data);
    this.status = "noticed";
    this.count = 0;
  }
  selfCheck() {
    const now = new Date().getTime();
    const interval = now - this.createTime;
    //大于30秒
    if (this.count < 3 && interval < 30 * 1000) {
      this.onChecked && this.onChecked(false);
      this.count++;
    } else {
      this.onChecked && this.onChecked(true);
      taskLogger.fatal({
        task: this
      })
    }
  }

}

exports.NoticedTask = NoticedTask;


class RefundTask extends Task {
  constructor(orderId, data) {
    super(orderId, data);
    this.status = "refund";
  }
  selfCheck() {
    const now = new Date().getTime();
    if (this.count > 3) {
      this.onChecked && this.onChecked(true);
      taskLogger.fatal({
        task: this
      })
    } else {
      this.onChecked && this.onChecked(false);
    }
    this.count++;
  }
}

exports.RefundTask = RefundTask;
